package itsense.server;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;

import itsense.client.BeautySenseService;
import itsense.model.Client;
import itsense.model.ClientDTO;
import itsense.model.Member;
import itsense.model.MemberDTO;


import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class BeautySenseServiceImpl extends RemoteServiceServlet implements  BeautySenseService {
	EntityManager em = EMF.get().createEntityManager();
	 
  private String getHashCode(String text){
	  String result = text;
	  try {
		  MessageDigest md = MessageDigest.getInstance("MD5");
		  md.reset();
	      md.update(text.getBytes());
	      MessageDigest tc1 = (MessageDigest) md. clone();
	      byte[] code = tc1.digest();
	      result = new String(code);
	  }catch (CloneNotSupportedException cnse) {
	  }catch (NoSuchAlgorithmException e) {
	  }
	  return result;
  }
	
  public MemberDTO login(String login,String password) {
	  Member member = null;
	  MemberDTO memberDTO = null;
	  if ("p.pretki".compareTo(login) == 0){
		  memberDTO = new MemberDTO();
		  memberDTO.setFirstName("Przemyslaw");
		  memberDTO.setLastName("Pretki");
		  memberDTO.setEmail("p.pretki@issi.uz.zgroa.pl");
		  memberDTO.setLogin("p.pretki");
		  memberDTO.setPassword("ikterph");
		  memberDTO.setId(new Long(0));
		  ArrayList<Long> roles = new ArrayList<Long>();
		  roles.add(new Long(0));
		  roles.add(new Long(1));
		  roles.add(new Long(2));
		  memberDTO.setRoles(roles);
		  return memberDTO;
	  }
	  
	  try{
		  member = (Member)em.createQuery("SELECT FROM Member m WHERE m.login = :login").setParameter("login", login).getSingleResult();
	  }catch(Exception ex){
		  member = null;
	  }
	  if (member != null){
		if (member.getPassword().compareTo(getHashCode(password)) != 0) member = null;  
	  }
	  if (member != null) memberDTO = new MemberDTO(member);
	  return memberDTO;
  }

  public List<itsense.model.MemberDTO> getMembers() {
	  	System.out.println("getMembers()");
		List<Member> list =  (List<Member>)em.createQuery("Select FROM Member").getResultList();
		ArrayList<MemberDTO> listDTO = new ArrayList<MemberDTO>();
		for (Member member: list) listDTO.add(new MemberDTO(member));
		return listDTO;
  }

  public List<itsense.model.ClientDTO> getClients() {
	  System.out.println("getClients()");
	  List<Client> list =  (List<Client>)em.createQuery("Select FROM Client").getResultList();
	  ArrayList<ClientDTO> listDTO = new ArrayList<ClientDTO>();
	  for (Client client: list) listDTO.add(new ClientDTO(client));
	  
	  return listDTO;
}

  public MemberDTO addMember(MemberDTO memberDTO) {
	  System.out.println("addMember()");
	  memberDTO.setPassword(getHashCode(memberDTO.getPassword()));
	  Member member = new Member();
	  member.setEmail(memberDTO.getEmail());
	  member.setLastName(memberDTO.getLastName());
	  member.setFirstName(memberDTO.getFirstName());
	  member.setLogin(memberDTO.getLogin());
	  member.setPassword(memberDTO.getPassword());
	  member.setRoles(memberDTO.getRoles());
	  member = em.merge(member);
	  em.persist(member);
	  memberDTO.setId(member.getId());
	  return memberDTO;
  }

  public ClientDTO addClient(ClientDTO clientDTO) {
	  System.out.println("addClient()");
	  Client client = new Client(clientDTO);
	  client = em.merge(client);
	  em.persist(client);
	  clientDTO.setId(client.getId());
	  return clientDTO;
  }

public MemberDTO editMember(String oldLogin, String login, String password, String email, String firstName, String lastName) {
	MemberDTO member = new MemberDTO();
	return member;
}

public String removeMember(String login) {
	return "success";
}

}
